<?php


namespace App\Http\Controllers\Admin;

use App\Http\Controllers\Controller;
use App\Models\NewRoles;
use App\Models\Admin;
use Illuminate\Http\Request;

class NewRolesController extends Controller
{
    public const STATUS_ENABLE = 1;

    public const STATUS_DISABLE = 2;

    public const STATUS = [
        self::STATUS_ENABLE     => '启用',
        self::STATUS_DISABLE    => '禁用'
    ];

    public function index(Request $request)
    {
        $page = [
            'page_name' => 'newRoles',
            'title'     => '角色管理'
        ];
        $roles = NewRoles::get();
        $title = '';

        return view('admin.newRoles.index', ['data' => $roles ?? [], 'title' => $title, 'page' => $page]);
    }

    public function edit(int $id)
    {
        $role = NewRoles::where('id', $id)->first();
        return view('admin.newRoles.edit', $role ?? []);
    }

    public function show(Request $request)
    {
        $roles = NewRoles::where()->first();
        return view('admin.newRoles', '');
    }

    public function add(Request $request)
    {

    }

    /**
     * 增加数据
     *
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function create(Request $request)
    {
        $simpleChineseName = $request->get('simple_chinese_name');
        $result = NewRoles::where("simple_chinese_name", $simpleChineseName)->first();
        if (!empty($result)) {
            return response()->json([
                'code'  =>  1,
                'msg'   => '简体角色名称已存在'
            ]);
        }
        $englishName = $request->get('english_name');
        $result = NewRoles::where("english_name", $englishName)->first();
        if (!empty($result)) {
            return response()->json([
                'code'  =>  1,
                'msg'   => '英文名称已存在'
            ]);
        }
        $traditionChineseName = $request->get('traditional_chinese_name');
        $result = NewRoles::where('traditional_chinese_name', $traditionChineseName)->first();
        if (!empty($result)) {
            return response()->json([
                'code'  =>  1,
                'msg'   => '繁体字名称已存在'
            ]);
        }

        $data = [
          'simple_chinese_name' => $simpleChineseName,
          'english_name'        => $englishName,
          'traditional_chinese_name'   => $traditionChineseName,
            'status'                   => self::STATUS_ENABLE,
        ];

        $insertResult = NewRoles::insert($data);
        if (!$insertResult) {
            return response()->json([
                'code'  =>  1,
                'msg'   => '添加失败'
            ]);
        }

        return response()->json([
            'code'  => 0,
            'msg'   => '添加成功'
        ]);
    }

    public function store(Request $request)
    {

    }

    public function editStatus(Request $request)
    {
        $id = $request->input('id');
        $role = NewRoles::where('id', $id)->first();
        if (empty($role)) {
            return ['status' => 'FAIL'];
        }

        $admin = Admin::where('role_id', $id)->where('status', 1)->first();
        if (!empty($admin)) {
            return ['status' => 'FAIL'];
        }

        $status = (int)$request->input('status');
        $status = $status === 1 ? 2 : 1;

        NewRoles::where('id', $id)->update(['status' => $status]);
        return ['status'=>'SUCCESS'];
    }
}